
var app = getApp();
// 右侧每一类的 bar 的高度（固定）
const RIGHT_BAR_HEIGHT = 20;
// 右侧每个子类的高度（固定）
const RIGHT_ITEM_HEIGHT = 100;
// 左侧每个类的高度（固定）
const LEFT_ITEM_HEIGHT = 50

Page({
  data: {
    hideNotice: false,
    notice: '',
    marqueePace: 1,//滚动速度
    marqueeDistance: 10,//初始滚动距离
    size: 12,
    interval: 20, // 时间间隔
    countTime: '',// 时间间隔
    "bnrUrl": [{
      "url": "../../images/1.jpg"
    }, {
      "url": "../../images/2.jpg"
    }, {
      "url": "../../images/3.jpg"
    }],
    //是否显示下面的购物车
    HZL_isCat: 0,

    //分类的数组
    HZL_categories: [
      '菜单',
      '评论',
      '商家'
    ],
    //swiper滑动的数组
    HZL_swiper_ID: 0,

    //模拟 数据
    goods: [],
    // 左 => 右联动 右scroll-into-view 所需的id
    HZL_toView: null,
    // 当前左侧选择的
    HZL_currentLeftSelect: null,
    // 右侧每类数据到顶部的距离（用来与 右 => 左 联动时监听右侧滚动到顶部的距离比较）
    HZL_eachRightItemToTop: [],
    HZL_leftToTop: 0
  },


  onLoad: function () {
    if (app.globalData.show != '') {
      var that = this;
      let data = {};
      var notice = app.globalData.adver;
      var length = that.data.notice.length * that.data.size; //文字长度
      var windowWidth = wx.getSystemInfoSync().windowWidth; // 屏幕宽度
      that.setData({ length, windowWidth });
      that.setData({
        marqueeDistance: windowWidth
      });
      that.run1();
      var goods = app.globalData.show;
      //导航栏的文字
      wx.setNavigationBarTitle({
        title: '预订',
      }),
        // 导航栏的文字颜色和背景的颜色
        wx.setNavigationBarColor({
          frontColor: '#000000',
          backgroundColor: '#00CCFF',
          animation: {
            duration: 400,
            timingFunc: 'easeIn'
          }
        })
      //高度大小
      wx.getSystemInfo({
        success: function (res) {
          var HZL_height = res.windowHeight - 260
          var HZL_height1 = res.windowHeight - 110
          that.setData({
            HZL_height: HZL_height,
            HZL_height1: HZL_height1
          })
        }
      });

      that.setData({
        goods: goods,
        HZL_currentLeftSelect: goods[0].typeId,
        HZL_eachRightItemToTop: this.HZL_getEachRightItemToTop(goods),
        notice: notice,
        HZL_num: app.globalData.HZL_num,
        HZL_my_cat: app.globalData.HZL_my_cat

      })
    } else {
     setTimeout(function(){

       var that = this;
       let data = {};
       var notice = app.globalData.adver;
       var length = that.data.notice.length * that.data.size; //文字长度
       var windowWidth = wx.getSystemInfoSync().windowWidth; // 屏幕宽度
       that.setData({ length, windowWidth });
       that.setData({
         marqueeDistance: windowWidth
       });
       that.run1();
       var goods = app.globalData.show;
       //导航栏的文字
       wx.setNavigationBarTitle({
         title: '预订',
       }),
         // 导航栏的文字颜色和背景的颜色
         wx.setNavigationBarColor({
           frontColor: '#000000',
           backgroundColor: '#00CCFF',
           animation: {
             duration: 400,
             timingFunc: 'easeIn'
           }
         })
       //高度大小
       wx.getSystemInfo({
         success: function (res) {
           var HZL_height = res.windowHeight - 260
           var HZL_height1 = res.windowHeight - 110
           that.setData({
             HZL_height: HZL_height,
             HZL_height1: HZL_height1
           })
         }
       });

       that.setData({
         goods: goods,
         HZL_currentLeftSelect: goods[0].typeId,
         HZL_eachRightItemToTop: this.HZL_getEachRightItemToTop(goods),
         notice: notice,
         HZL_num: app.globalData.HZL_num,
         HZL_my_cat: app.globalData.HZL_my_cat

       })
     },1000)
    } 
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.searchbar = this.selectComponent("#searchbar");
  },
  run1: function () {
    var that = this;
    that.data.countTime = setInterval(function () {
      if (-that.data.marqueeDistance < that.data.length) {
        that.setData({
          marqueeDistance: that.data.marqueeDistance - that.data.marqueePace,
        });
      } else {
        clearInterval(that.data.countTime);
        that.setData({
          marqueeDistance: that.data.windowWidth
        });
        that.run1();
      }
    }, that.data.interval);
  },
  onShow: function () {


  },
  //记录swiper滚动的
  HZL_swiperchange: function (e) {
    var that = this;
    that.setData({
      HZL_swiper_ID: e.detail.current,
    })
  },
  //点击分类栏
  HZL_categories: function (e) {
    var that = this;
    that.setData({
      HZL_swiper_ID: e.currentTarget.dataset.index
    })
  },

  // 获取每个右侧的 bar 到顶部的距离，用来做后面的计算。
  HZL_getEachRightItemToTop: function (e) {
    var obj = {};
    var totop = 0;
    // 右侧第一类肯定是到顶部的距离为 0
    obj[e[0].typeId] = totop
    // 循环来计算每个子类到顶部的高度
    for (let i = 1; i < (e.length + 1); i++) {
      totop += (RIGHT_BAR_HEIGHT + e[i - 1].wxSpecies.length * RIGHT_ITEM_HEIGHT)
      // 这个的目的是 例如有两类，最后需要 0-1 1-2 2-3 的数据，所以需要一个不存在的 'last' 项，此项即为第一类加上第二类的高度。
      obj[e[i] ? e[i].typeId : 'last'] = totop
    }
    return obj
  },
  // 监听右侧的滚动事件与 HZL_eachRightItemToTop 的循环作对比 从而判断当前可视区域为第几类，从而渲染左侧的对应类。
  right: function (e) {
    for (let i = 0; i < this.data.goods.length; i++) {
      let left = this.data.HZL_eachRightItemToTop[this.data.goods[i].typeId]
      let right = this.data.HZL_eachRightItemToTop[this.data.goods[i + 1] ? this.data.goods[i + 1].typeId : 'last']
      if (e.detail.scrollTop < right && e.detail.scrollTop >= left) {
        this.setData({
          HZL_currentLeftSelect: this.data.goods[i].typeId,
          HZL_leftToTop: LEFT_ITEM_HEIGHT * i
        })
      }
    }
  },
  // 左侧类的点击事件，点击时，右侧会滚动到对应分类
  left: function (e) {
    this.setData({
      HZL_toView: e.target.id,
      HZL_currentLeftSelect: e.target.id
    })
  },


  //是否显示下面的购物车
  HZL_isCat: function (e) {
    var that = this;
    if (that.data.HZL_isCat == 0 && that.data.HZL_num > 0) {
      that.setData({
        HZL_isCat: 1
      })
    } else if (that.data.HZL_isCat == 1 && that.data.HZL_num > 0) {
      that.setData({
        HZL_isCat: 0
      })
    }
  },

  //关闭
  HZL_isCat_close: function (e) {
    this.setData({
      HZL_isCat: 0
    })
  },

  //清空
  HZL_zero: function (e) {
    for (var i = 0; i < this.data.goods.length; i++) {
      for (var j = 0; j < this.data.goods[i].wxSpecies.length; j++) {
        this.data.goods[i].wxSpecies[j].wxSpeciesCount = 0
      }
    }
    this.setData({
      HZL_isCat: 0,
      HZL_num: 0,
      HZL_my_cat: [],
      goods: this.data.goods,
    })
  },

  // 增加
  HZL_jia: function (e) {
    var index = e.currentTarget.dataset.index;
    var parentIndex = e.currentTarget.dataset.parentindex;
    var HZL_my_cat = this.HZL_my_jia(parentIndex, index)
    this.setData({
      HZL_num: this.data.HZL_num,
      HZL_my_cat: HZL_my_cat,
      goods: this.data.goods,
    })
  },

  //减少
  HZL_jian: function (e) {
    var index = e.currentTarget.dataset.index;
    var parentIndex = e.currentTarget.dataset.parentindex;
    var HZL_my_cat = this.HZL_my_jian(parentIndex, index)

    if (this.data.HZL_num == 0) {
      this.data.HZL_isCat = 0;
    } else {
      this.data.HZL_isCat = 1;
    }

    this.setData({
      HZL_num: this.data.HZL_num,
      HZL_my_cat: HZL_my_cat,
      goods: this.data.goods,
    })
  },

  // 下面购物车增加
  HZL_jia1: function (e) {
    var index = e.currentTarget.dataset.index;
    var parentIndex = e.currentTarget.dataset.parentindex;
    var HZL_my_cat = this.HZL_my_jia(parentIndex, index)
    this.setData({
      HZL_num: this.data.HZL_num,
      HZL_my_cat: HZL_my_cat,
      goods: this.data.goods,
    })
  },

  //下面购物车减少
  HZL_jian1: function (e) {
    var index = e.currentTarget.dataset.index;
    var parentIndex = e.currentTarget.dataset.parentindex;
    var HZL_my_cat = this.HZL_my_jian(parentIndex, index)

    if (this.data.HZL_num == 0) {
      this.data.HZL_isCat = 0;
    } else {
      this.data.HZL_isCat = 1;
    }

    this.setData({
      HZL_num: this.data.HZL_num,
      HZL_my_cat: HZL_my_cat,
      goods: this.data.goods,
      HZL_isCat: this.data.HZL_isCat
    })
  },

  //封装加的方法
  HZL_my_jia: function (parentIndex, index) {
    this.data.HZL_num++;
    var index = index;
    var parentIndex = parentIndex;
    var id = this.data.goods[parentIndex].wxSpecies[index].wxSpeciesId;
    var name = this.data.goods[parentIndex].wxSpecies[index].wxSpeciesName;
    var price = this.data.goods[parentIndex].wxSpecies[index].wxSpeciesPrice;
    var num = ++this.data.goods[parentIndex].wxSpecies[index].wxSpeciesCount;
    //弄一个元素判断会不会是重复的
    var mark = 'a' + index + 'b' + parentIndex + 'c' + '0' + 'd' + '0'
    var obj = { num: num, name: name, mark: mark, index: index, parentIndex: parentIndex, price: price, id: id };
    var HZL_my_cat = this.data.HZL_my_cat;
    HZL_my_cat.push(obj)

    var arr = [];
    //去掉重复的
    for (var i = 0; i < HZL_my_cat.length; i++) {
      if (obj.name == HZL_my_cat[i].name) {
        HZL_my_cat.splice(i, 1, obj)
      }
      if (arr.indexOf(HZL_my_cat[i]) == -1) {
        arr.push(HZL_my_cat[i]);
      }
    }
    app.globalData.HZL_num = this.data.HZL_num;
    app.globalData.HZL_my_cat = this.data.HZL_my_cat;

    return arr
  },

  //封装减的方法
  HZL_my_jian: function (parentIndex, index) {
    this.data.HZL_num--;
    var index = index;
    var parentIndex = parentIndex;
    var id = this.data.goods[parentIndex].wxSpecies[index].wxSpeciesId;
    var name = this.data.goods[parentIndex].wxSpecies[index].wxSpeciesName;
    var price = this.data.goods[parentIndex].wxSpecies[index].wxSpeciesPrice;
    var num = --this.data.goods[parentIndex].wxSpecies[index].wxSpeciesCount;
    //弄一个元素判断会不会是重复的
    var mark = 'a' + index + 'b' + parentIndex + 'c' + '0' + 'd' + '0'
    var obj = { num: num, name: name, mark: mark, index: index, parentIndex: parentIndex, price: price, id: id };
    var HZL_my_cat = this.data.HZL_my_cat;
    HZL_my_cat.push(obj)

    var arr = [];
    //去掉重复的
    for (var i = 0; i < HZL_my_cat.length; i++) {
      if (obj.name == HZL_my_cat[i].name) {
        HZL_my_cat.splice(i, 1, obj)
      }
    }
    app.globalData.HZL_num = this.data.HZL_num;
    app.globalData.HZL_my_cat = this.data.HZL_my_cat;

    var arr1 = [];
    //当数量大于1的时候加
    for (var i = 0; i < HZL_my_cat.length; i++) {
      if (arr1.indexOf(HZL_my_cat[i]) == -1) {
        arr1.push(HZL_my_cat[i]);
        if (HZL_my_cat[i].num > 0) {
          arr.push(arr1[i])
        }
      }
    }

    return arr
  },
  Settlement: function (e) {
    var arr = [];
    for (var i = 0; i < this.data.HZL_my_cat.length; i++) {
      for (var y = 0; y < this.data.HZL_my_cat.length; y++) {
        if (this.data.HZL_my_cat[y].name == this.data.HZL_my_cat[i].name) {
          this.data.HZL_my_cat.splice(y, 1, this.data.HZL_my_cat[y]);
        }
        if (arr.indexOf(this.data.HZL_my_cat[i]) == -1) {
          arr.push(this.data.HZL_my_cat[i]);
        }
      }
    }
    wx.navigateTo({
      url: '../../components/settlement/settlement?goods=' + JSON.stringify(arr)
    });
  },
  setdate: function () {
    var car = {
      HZL_num: this.data.HZL_num,
      HZL_my_cat: this.data.HZL_my_cat,
    }
    this.searchbar.setCat(car);
  }
})